<?php
// +----------------------------------------------------------------------
// | CoreThink [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.corethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: jry <598821125@qq.com> <http://www.corethink.cn>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Think\Controller;
use Com\DingdingAuth;
/**
 * 后台用户控制器
 * @author jry <598821125@qq.com>
 */
class EmployeeController extends AdminController{
	/**
	 * 员工绑定钉钉
	 */
	public function dingding(){
		$corpid=C('CORPID');
		$corpsecret=C('SECRET');
		$D=new DingdingAuth($corpid, $corpsecret);
		$token = $D->getAccessToken();
		
		//获取部门列表
		$department_re=$D->getDingRe("department/list",'','GET','');
		//dump($department_re);exit;
		
		if ($department_re['errmsg']=='ok') {
			foreach ($department_re['department'] as $k => $v){
				//获取部门成员userid
				$param=array(
						"department_id"=>$v['id']//部门id
				);
				$user_re=$D->getDingRe("user/simplelist",'','GET',$param);
				if ($user_re['errmsg']=='ok') {
					foreach ($user_re['userlist'] as $k2=>$v2){
						
						M('User')->where(array('username'=>$v2['name']))->setfield('ding_id',$v2['userid']);
					}
				}else{
					$this->error($user_re['errmsg']);
				}
				
			}
	
			$this->success('绑定钉钉成功！');
		}else{
			$this->error($department_re['errmsg']);
		}
	}
	/**
	 * 默认首页
	 */
		public function index(){
			
			  $keywords = I('keywords');
        if (! empty($keywords)) {
            $map['department'] = $keywords;
        }
			
		 //搜索
        $keyword = I('keyword', '', 'string');
        $condition = array('like','%'.$keyword.'%');
        $map['uid|email|name|mobile|qq'] = array($condition, $condition,$condition, $condition,$condition,'_multi'=>true);

        //获取所有用户
       $map['status'] = array('egt', '0'); //禁用和正常状态
        $data_list = D('Employee')->page(!empty($_GET["p"])?$_GET["p"]:1, C('ADMIN_PAGE_ROWS'))->where($map)->order('id desc')->select();
        $page = new \Common\Util\Page(D('Employee')->where($map)->count(), C('ADMIN_PAGE_ROWS'));

        //改变$data_list数组，把customerid数字值对应成汉字名字
   foreach($data_list as $key=>&$val){
        
	   $a=$val['department'];
	   $val['department']=M("UserGroup")->where(array("id"=>$a))->getField("title");
		$val['name']='<a href="'.U('Admin/Employee/xq', array('id' => $val['id'])).'" style="text-decoration:none;">'.$val['name'].'</a>';
        $val['post'] = get_descriptions_title('employee', 'pos',$val['post']);
		
		switch($val['sex']){
			case "1":
				$val['sex']="男";
				break;
				case "-1":
				$val['sex']="女";
				break;
					case "0":
				$val['sex']="保密";
				break;	
		}
		
   }
   //客户（企业）管理员不可新增网站，新增按钮样式修改
   $top_button['title']='一键绑定钉钉';
   $top_button['class']="btn btn-primary";
   $top_button['href']=U('Admin/Employee/dingding');
//      $attr['title'] = '员工详情';
//      $attr['class'] = 'label label-xq';
//      $attr['href']  = U('Admin/Employee/xq', array('id' => '__data_id__'));

   //设为管理员 or取消管理员
   $admin_button['field'] = 'ding_pass';
   $admin_button['0']['title']='设置钉钉权限';
   $admin_button['0']['class']="label label-success ajax-get confirm";
   $admin_button['0']['href']=U('setdingpass', array('status' =>'set','id' => '__data_id__'));
   $admin_button['1']['title']='取消钉钉权限';
   $admin_button['1']['class']="label label-warning ajax-get confirm";
   $admin_button['1']['href']=U('setdingpass', array('status' =>'del','id' => '__data_id__'));
$part=M("UserGroup")->where("id>1")->field("id as fieldvalue,title ")->select();

        //使用Builder快速建立列表页面。
        $builder = new \Common\Builder\ListBuilder();
        $builder->setMetaTitle('项目信息列表') //设置页面标题
         ->addTopButton('addnew')  //添加新增按钮
		//	->addTopButton('resume')  //添加启用按钮
		//	->addTopButton('forbid')  //添加禁用按钮
			->addTopButton('delete')  //添加删除按钮
			->addTopButton('self',$top_button)  //添加绑定钉钉按钮
			->setSearch('请输入员工ID/姓名/手机/邮箱', U('index'),$part)
			->addTableColumn('uid', '员工UID')
			->addTableColumn('name', '姓名')
			->addTableColumn('sex','性别')
			->addTableColumn('employee_num', '员工编号')
			->addTableColumn('department', '部门')
			->addTableColumn('post','职务')
			->addTableColumn('mobile', '手机号')
	 		
			->addTableColumn('qq','qq')
	//		->addTableColumn('wechat','微信号')
	//		->addTableColumn('phone','办公电话')
	//		->addTableColumn('email', '邮箱')
		
           ->addTableColumn('birthday', '生日')
//           ->addTableColumn('birth_place', '出生地')
//			->addTableColumn('education','学历')
//			->addTableColumn('school','毕业院校')
//			->addTableColumn('create_time','创建时间')
//			->addTableColumn('create_member','创建人ID')
 			//->addTableColumn('remark','备注')
 			->addTableColumn('ding_pass', '钉钉权限', 'status')
//            ->addTableColumn('create_time', '创建时间','time')
//             ->addTableColumn('status', '状态', 'status')
			->addTableColumn('right_button', '操作', 'btn')
			->setTableDataList($data_list) //数据列表
			->setTableDataPage($page->show()) //数据列表分页
			->addRightButton('edit')   //添加编辑按钮
			->addRightButton('forbid') //添加禁用/启用按钮
	//		->addRightButton('delete') //添加删除按钮
	//		->addRightButton('self',$attr) //添加员工详情按钮
        	->addRightButton('caseButton','',$admin_button)
			->display();
			}
		//新增员工信息
	public function add(){
		$department=D('UserGroup')->field('id,title')->select();
		foreach($department as $ke=>$ve){
			$depart[$ve['id']]=$ve['title'];
		}
		
		
		$uid=I('id');
	     $id=is_login();
		$userinfo=get_user_info($uid);
		
		if(IS_POST){
			
            $project_object = D('Employee');
            $data = $project_object->create();
            if($data){
                $id = $project_object->add();
                if($id){
                    M('User')->where("id=$data[uid]")->setField('group',$data['department']);
                    $this->success('新增成功', U('index'));
                }else{
                    $this->error('新增失败');
                }
            }else{
                $this->error($project_object->getError());
            }
        }else{
        	$pos = get_descriptions_title('employee', 'pos');
        	
            //使用FormBuilder快速建立表单页面。
            $builder = new \Common\Builder\FormBuilder();
            $builder->setMetaTitle('新增项目信息') //设置页面标题
                    ->setPostUrl(U('add')) //设置表单提交地址
                  ->addFormItem('reg_type', 'hidden', '注册方式', '注册方式')
           //     ->addFormItem('usertype', 'radio', '用户类型', '用户类型', C('USER_TYPE_LIST'))
                    ->addFormItem('uid','text', '会员ID', '填写前台注册会员的ID')
		            ->addFormItem('employee_num','text', '工号', '员工编号')
		            ->addFormItem('department','select', '部门', '部门',$depart)			 
					->addFormItem('name','text','姓名','填写真实姓名')
					->addFormItem('sex','radio','性别','性别',C('USER_SEX_LIST'))
					->addFormItem('post','select','职务','职务',$pos)
					->addFormItem('qq','text','qq','qq')
					->addFormItem('wechat','text','微信号','微信号')
		            ->addFormItem('phone','text', '办公电话', '办公电话') 
					->addFormItem('mobile','text','手机号','手机号')  
					 ->addFormItem('email','text', '邮箱', '邮箱')
		            ->addFormItem('birthday','text', '生日', '生日')
		            ->addFormItem('birth_place','text', '出生地', '出生地')			 
					->addFormItem('education','select','学历','学历',array('小学','初中','高中','大专','本科','研究生','博士'))
					->addFormItem('school','text','毕业院校','毕业院校')
					->addFormItem('create_member','hidden','创建人ID','创建人ID')
					->addFormItem('remark','textarea','备注','备注')				  
                   ->setFormData(array('uid' => $uid,'create_member' =>$id,'mobile'=>$userinfo['mobile'],'email'=>$userinfo['email'],'birthday'=>date('Y-m-d',$userinfo['birthday']))) //注册方式为后台添加
                  
                    ->display();
        }
		
}

	/**
     * 编辑
     * @author jry <598821125@qq.com>
     */
    public function edit($id){
    			$department=D('UserGroup')->field('id,title')->select();
		foreach($department as $ke=>$ve){
			$depart[$ve['id']]=$ve['title'];
		}
        //获取信息
        $info = D('Employee')->find($id);
     
        if(IS_POST){
            $project_object = D('Employee');
           
            if($project_object->save($_POST)){
                $this->success('更新成功', U('index'));
            }else{
                $this->error('更新失败', $project_object->getError());
            }
        }else{
            $info = D('Employee')->find($id);
            $pos = get_descriptions_title('employee', 'pos');
            $info['create_member']=get_user_info($info['create_member'],"username");
            //使用FormBuilder快速建立表单页面。
            $builder = new \Common\Builder\FormBuilder();
            $builder->setMetaTitle('编辑') //设置页面标题
                    ->setPostUrl(U('edit')) //设置表单提交地址
                    ->addFormItem('id', 'hidden', 'ID', 'ID')
                 //   ->addFormItem('usertype', 'radio', '用户类型', '用户类型', C('USER_TYPE_LIST'))
                   ->addFormItem('uid','text', '员工ID', '员工ID')
		            ->addFormItem('employee_num','text', '员工编号', '员工编号')
		            ->addFormItem('department','select', '部门', '部门',$depart)			 
					->addFormItem('name','text','姓名','姓名')
					->addFormItem('sex','radio','性别','性别',C('USER_SEX_LIST'))
					->addFormItem('post','select','职务','职务',$pos)
					->addFormItem('qq','text','qq','qq')
					->addFormItem('wechat','text','微信号','微信号')
		            ->addFormItem('phone','text', '办公电话', '办公电话') 
					->addFormItem('mobile','text','手机号','手机号')  
					   ->addFormItem('email','text', '邮箱', '邮箱')
		            ->addFormItem('birthday','text', '生日', '生日')
		            ->addFormItem('birth_place','text', '出生地', '出生地')			 
					->addFormItem('education','select','学历','学历',array('小学','初中','高中','大专','本科','研究生','博士'))
					->addFormItem('school','text','毕业院校','毕业院校')
					->addFormItem('create_member','text','创建人ID','创建人ID')
					->addFormItem('remark','textarea','备注','备注')
                    ->setFormData($info)
                    ->display();
        }
    }
		
		

// public function xq($id){
// 	    			$department=D('UserGroup')->field('title')->select();
//	 $info = D('Employee')->find($id);
//	 
//          //使用FormBuilder快速建立表单页面。
//          $builder = new \Common\Builder\FormBuilderxq();
//          $builder->setMetaTitle('员工详情') //设置页面标题
//                  ->setPostUrl(U('edit')) //设置表单提交地址
//                  ->addFormItem('id', 'hidden', 'ID', 'ID')
//               //   ->addFormItem('usertype', 'radio', '用户类型', '用户类型', C('USER_TYPE_LIST'))
//                ->addFormItem('uid','text', '员工ID', '员工ID')
//		            ->addFormItem('employee_num','text', '员工编号', '员工编号')
//		            ->addFormItem('department','select', '部门', '部门',$department)			 
//					->addFormItem('name','text','姓名','姓名')
//					->addFormItem('sex','radio','性别','性别',array('女','男','保密'))
//					->addFormItem('post','text','职务','职务')
//					->addFormItem('qq','text','qq','qq')
//					->addFormItem('wechat','text','微信号','微信号')
//		            ->addFormItem('phone','text', '办公电话', '办公电话') 
//					->addFormItem('mobile','text','手机号','手机号')  
//					   ->addFormItem('email','text', '邮箱', '邮箱')
//		            ->addFormItem('birthday','text', '生日', '生日')
//		            ->addFormItem('birth_place','text', '出生地', '出生地')			 
//					->addFormItem('education','select','学历','学历',array('小学','初中','高中','大专','本科','研究生','博士'))
//					->addFormItem('school','text','毕业院校','毕业院校')
//					->addFormItem('create_member','text','创建人ID','创建人ID')
//					->addFormItem('remark','textarea','备注','备注')
//                  ->setFormData($info)
//					
//                  ->display();
// }
		//客户详情	
 public function xq($id){
			$map['id']=$id;
			$data_list = D('Employee')->page(!empty($_GET["p"])?$_GET["p"]:1, C('ADMIN_PAGE_ROWS'))->where($map)->order('id desc')->select();
			foreach($data_list as $key=>&$val){
				
		    $a=$val['department'];
	          $val['department']=M("UserGroup")->where(array("id"=>$a))->getField("title");
			
			 $val['post'] = get_descriptions_title('employee', 'pos',$val['post']);
				
			/*$department=D('UserGroup')->field('title')->select();
			$a=$val['department'];
			$val['department']=$department[$a]['title'];*/
			$val['create_time']=date('Y/m/d',$val['create_time']);
			$val['create_member']=get_employee_info($val['create_member'],'name');
			switch($val['sex']){
			case '1':
			$val['sex']="男";
			break;
			case '-1':
			$val['sex']="女";
			break;
			case '0':
			$val['sex']="保密";
			break;
			}
			switch($val['education']){
			case '0':
			$val['education']="小学";
			break;
			case '1':
			$val['education']="初中";
			break;
			case '2':
			$val['education']="高中";
			break;
			case '3':
			$val['education']="大专";
			break;
			case '4':
			$val['education']="本科";
			break;
			case '5':
			$val['education']="研究生";
			break;
			case '6':
			$val['education']="博士";
			break;
			}
			}
			$builder = new \Common\Builder\ListBuilderxq();
			$builder->setMetaTitle('员工详情') //设置页面标题
			->addTableGroup('zu_one','uid', '员工UID','1')
			->addTableItem('zu_one','name', '姓名','4')
			->addTableItem('zu_one','employee_num', '员工编号','4')
			->addTableItem('zu_one','department', '部门','4')
			->addTableItem('zu_one','sex','性别','4')
			->addTableItem('zu_one','post','职务','4')
			->addTableItem('zu_one','qq','qq','4')
			->addTableItem('zu_one','wechat','微信号','4')
			->addTableItem('zu_one','phone','办公电话','4')
			->addTableItem('zu_one','mobile', '手机号','4')
			->addTableItem('zu_one','email', '邮箱','4')
			->addTableGroup('zu_two','head_two', '其他信息','1')
			->addTableItem('zu_two','birthday', '生日','4')
			->addTableItem('zu_two','birth_place', '出生地','4')
			->addTableItem('zu_two','education','学历','4')
			->addTableItem('zu_two','school','毕业院校','4')
			->addTableItem('zu_two','create_time','创建时间','4')
			->addTableItem('zu_two','create_member','创建人','4')
			->addTableGroup('zu_three','remark','备注','2')
			->setTableDataList($data_list)
			//数据列表
			->display();

			}

			/**
			 * 设置钉钉权限
			 */
			public function  setdingpass(){
				$m_object = M('Employee');
				$id=I('id');
				$status=I('status');
				if ($status=='set') {
					$re=$m_object->where('id='.$id)->setField('ding_pass',1);
					if($re)
					{
						$this->success('设置钉钉权限成功!', U('index'));
					}else {
						$this->error('设置钉钉权限失败!');
					}
				}else{
					$re=$m_object->where('id='.$id)->setField('ding_pass',0);
					if($re)
					{
						$this->success('取消钉钉权限成功!', U('index'));
					}else {
						$this->error('取消钉钉权限失败!');
					}
				}
			
			}		
			
			
			
}
	